package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.PageVo;
import com.itheima.admin.dto.RoleAuthDto;
import com.itheima.admin.dto.RoleDto;
import com.itheima.admin.dto.RolePageDto;
import com.itheima.admin.mapper.RoleMapper;
import com.itheima.admin.pojo.Role;
import com.itheima.admin.service.IRoleService;
import com.itheima.admin.vo.RoleAuthVo;
import com.itheima.admin.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WXL
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageVo<RoleVo> queryByPage(RolePageDto rolePageDto) {

        Page<Role> page = new Page<>(rolePageDto.getCurrentPage(),rolePageDto.getPageSize());

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(rolePageDto.getName()!=null){
            queryWrapper.like("name",rolePageDto.getName());
        }


        IPage<Role> rolePage = roleMapper.selectPage(page, queryWrapper);

        //List<Role> ==> List<RoleVo>
        List<RoleVo> collect = getVos(rolePage.getRecords());

        System.out.println(collect);
        return new PageVo<>(collect, (int) rolePage.getTotal());
    }

    private List<RoleVo> getVos(List<Role> roleList) {
        if (roleList == null){
            return new ArrayList<>();
        }
        //List<Role> ==> List<RoleVo>
       return roleList.stream().map(
                role -> {
                    RoleVo roleVo = new RoleVo();
                    BeanUtils.copyProperties(role, roleVo);
                    Date createTime = role.getCreateTime();
                    roleVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(createTime));
                    return roleVo;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public List<RoleVo> queryAll() {
        return getVos(roleMapper.selectList(null));
    }

    @Override
    public boolean addRole(RoleDto roleDto) {
        return roleMapper.insert(roleDto.toRole()) > 0;
    }

    @Override
    public boolean deleteRole(String roleId) {
        //1.先删除角色和用户对应关系
        roleMapper.deleteRoleUser(roleId);
        //2.再删除角色和模块对应关系
        roleMapper.deleteRoleModule(roleId);
        //3.最后再删除角色
        return roleMapper.deleteById(roleId) > 0;
    }

    @Override
    public boolean roleAuth(RoleAuthDto roleAuthDto) {
        List<RoleAuthVo> list = new ArrayList<>();
        for (int i=0; i< roleAuthDto.getModuleIds().size(); i++){
            list.add(new RoleAuthVo(roleAuthDto.getRoleId(),roleAuthDto.getModuleIds().get(i)));
        }

        return roleMapper.addRoleToModule(list);
    }

    @Override
    public RoleVo queryById(String roleId) {
        Role role = roleMapper.selectById(roleId);
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role,roleVo);
        return roleVo;
    }

    @Override
    public boolean editRole(RoleVo roleVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVo,role);
        return roleMapper.updateById(role) > 0;
    }

    @Override
    public List<String> queryModulesById(String roleId) {


        return roleMapper.queryModulesById(roleId);
    }
}
